package cn.wetcland.task;

import cn.TCSP;
import cn.wetcland.panel.GlobalMarketPanel;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.Calendar;
import java.util.Collection;

public class GlobalMarketUpdateTask {

    /**
     * 开启任务
     * 每分钟检测一次时间
     * 每天 00:00 更新全球市场的物品
     */
    public void runTask() {
        // 服务器开启后先更新一次
        GlobalMarketPanel.fillGlobalMarketInventories();
        Bukkit.getScheduler().runTaskTimerAsynchronously(TCSP.getTcsp(), () -> {
            Calendar instance = Calendar.getInstance();
            int hourOfDay = instance.get(Calendar.HOUR_OF_DAY);
            int minute = instance.get(Calendar.MINUTE);
            if (hourOfDay == 0 && minute == 0) {
                // 同步
                Bukkit.getScheduler().runTask(TCSP.getTcsp(), () -> {
                    Collection<? extends Player> onlinePlayers = Bukkit.getOnlinePlayers();
                    for (Player player : onlinePlayers) {
                        player.closeInventory();
                        player.sendTitle("§c强制关闭菜单", "§a服务器正在更新全球市场~", 10, 10, 10);
                        player.playSound(player.getLocation(), "wetcland.bgm_heixiu", 5f, 1f);
                    }
                    GlobalMarketPanel.fillGlobalMarketInventories();
                    Bukkit.broadcastMessage("§b===============");
                    Bukkit.broadcastMessage("§d全球市场已更新!");
                    Bukkit.broadcastMessage("§b===============");
                });
            }
        }, 0, 20 * 60);
    }

}
